<?php

	require('simplepie.inc');
	
	

	class FTARSSFeedReader extends FTAObject {
		
		
		//var feed reader object.. 
		var $feed;
		var $feedCache;
		 
		function FTARSSFeedReader() {
			global $FTARSSFeedReaderEnableCache;
			
			parent::FTAObject();
			
			$this->feed = new SimplePie();
			$this->feed->enable_cache(
				$FTARSSFeedReaderEnableCache ? true : false
			);
			$this->feed->enable_order_by_date(true);
			$this->feed->set_cache_location( CMS_BINARY_CACHE_PATH );
			
			
			$this->feedCache = array();
		}
		
		

		/**
		 * reads the feed and maps the objects into simple items.. that are not the same
		 * as the internal representation
		 * @return unknown_type
		 */
		function readFeed($feedUrl){

			$newsItems = extractObjectWithKeyFromArray($feedUrl,$this->feedCache,null);
			

			if (is_null($newsItems)) {
				
				$this->feed->set_feed_url($feedUrl);	
				$this->feed->init(); 		
				$feedItems = $this->feed->get_items();
				
				$newsItems = array();
				
				//print_r(count($feedItems));
				foreach ($feedItems as $feedItem){
					//echo "x";
					//myPrintR();
					$o = new stdClass();
					
					$o->documentyear = $feedItem->get_date("Y-m-d H:i:s");
					$o->title = $feedItem->get_title();
					$o->link = $feedItem->get_permalink();
					$o->content = $feedItem->get_description();
					
					$commentCount = $feedItem->get_item_tags('http://purl.org/rss/1.0/modules/slash/','comments');
					if (@$commentCount[0]['data'])
						$o->commentsCount = $commentCount[0]['data'];
					else 
						$o->commentsCount = 0;
						
					$commentUrl = $feedItem->get_item_tags('','comments');
					if (@$commentUrl[0]['data'])
						$o->commentUrl = $commentUrl[0]['data'];
					else 
						$o->commentUrl = $o->link;
					
					array_push($newsItems, $o);
				
				}
				
			}
			return $newsItems;
		}
		
		/** 
		 * renders the whole feed.. 
		 * @return unknown_type
		 */
		function renderFullFeed() {
			
		}
		
		function renderLatestNewsItemDetailed($feedUrl){
			// we assume the read feed returns.. the latest item..  sorted.. so we get the feed items and take the first one

			
			$items = $this->readFeed($feedUrl);
			if (0 < $items){
				$this->renderNewsItemDetailed($items[0]);
			}
		}

		function renderNewsItemDetailed($item){
?>
<div class="newsItem">
<h2 class="sifr"><a href="<?= $item->link ?>"><?= $item->title ?></a></h2>
<h3 class="sifr"><?= date( 'l j F ', strtotime($item->documentyear)); ?></h3>
<?= $item->content ?>
&nbsp;<a href="<?= $item->link ?>">Read More</a>
 <a href="<?= $item->commentUrl ?>" class="journalComments"><?= $item->commentsCount ?> comments</a>
</div>
<?php 
			
		}
	}
	
	
	
//	function fta_getNewsFeed($feedUrl){
//		ob_start();
//		$o =  &fta_getSharedRSSFeedReaderObject();
//		$o->renderNewsItemDetailed($feedUrl);
//		return ob_get_clean();		
//	}
	
	function fta_getLatestNewsItemDetailed($feedUrl){
		ob_start();
		$o =  &fta_getSharedRSSFeedReaderObject();
		$o->renderLatestNewsItemDetailed($feedUrl);
		return ob_get_clean();
	}

	
	/**
	 * Gets the fta obejct.. these are singletons really..
	 *
	 * @return FTAWebPage
	 */
	function &fta_getSharedRSSFeedReaderObject(){

		global $fta_;

		global $FTARSSReaderClass;
		
		if(!is_null($FTARSSReaderClass) && "FTARSSFeedReader" != $FTARSSReaderClass){
		} else {
			$FTARSSReaderClass = "FTARSSFeedReader";
		}
			
		if (!array_key_exists('ftaRSSFeedReader',$fta_)){
			$fta_->ftaRSSFeedReader = new  $FTARSSReaderClass();
		}
		return $fta_->ftaRSSFeedReader;
	}
	